<template>
  <div v-loading.lock="loading"
    class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm"
        :inline="true"
        :model="queryForm">
        <el-form-item label="仓库编码"
          prop="code">
          <el-input v-model="queryForm.code"
            placeholder="仓库编码" />
        </el-form-item>
        <el-form-item label="仓库名称"
          prop="name">
          <el-input v-model="queryForm.name"
            placeholder="仓库名称" />
        </el-form-item>
        <el-form-item label="仓库类型"
          prop="type">
          <el-select v-model="queryForm.type">
            <el-option label="全部"
              :value="0" />
            <el-option label="危化品仓库"
              :value="1" />
            <el-option label="物料仓库"
              :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态"
          prop="status">
          <el-select v-model="queryForm.status">
            <el-option label="全部"
              :value="0" />
            <el-option label="启用"
              :value="1" />
            <el-option label="禁用"
              :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
            @click="onQuery">查询</el-button>
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button v-rights="'Create'"
          icon="el-icon-plus"
          type="primary"
          @click="onNew">新增</el-button>
      </div>

      <el-table :data="list"
        element-loading-text="Loading"
        tooltip-effect="dark"
        style="width: 100%"
        height="calc(100% - 150px)">
        <el-table-column align="center"
          label="序号"
          width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="仓库编码">
          <template slot-scope="scope">
            {{ scope.row.code }}
          </template>
        </el-table-column>
        <el-table-column label="仓库名称">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="仓库类型">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? '危化品仓库' : '物料仓库' }}
          </template>
        </el-table-column>
        <el-table-column label="负责人">
          <template slot-scope="scope">
            {{ scope.row.personLiableName }}
          </template>
        </el-table-column>
        <el-table-column label="联系电话">
          <template slot-scope="scope">
            {{ scope.row.contactPhone }}
          </template>
        </el-table-column>
        <el-table-column label="仓库地址">
          <template slot-scope="scope">
            {{ scope.row.address }}
          </template>
        </el-table-column>
        <el-table-column label="仓库描述"
          show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.description }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.status == 1 ? '启用' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作人">
          <template slot-scope="scope">
            {{ scope.row.operatorName }}
          </template>
        </el-table-column>
        <el-table-column label="操作时间">
          <template slot-scope="scope">
            {{ scope.row.updateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link v-rights="'Update'"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)">编辑</el-link>
              <el-link v-rights="'Delete'"
                size="mini"
                type="danger"
                style="display: none;"
                @click="handleDelete(scope.row)">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange" />
    </div>

    <StorageEdit ref="StorageEdit"
      v-model="dialogFormVisible"
      :edit-id="editId"
      @success="DialogCall" />
  </div>
</template>

<script>
import { deleteRecord, getPageList } from "../../../api/Storage/Storage.js";
import StorageEdit from "./StorageEdit.vue";

export default {
  name: "StorageList",
  components: { StorageEdit },
  data() {
    return {
      loading: true,
      editId: "",
      list: [],
      queryForm: {
        name: "",
        code: "",
        type: 0,
        address: "",
        personLiableId: 0,
        contactPhone: "",
        status: 1,
        description: "",
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getPageList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.editId = "";
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.editId = rowData.id;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
    // 关闭窗体回调函数
    DialogCall() {
      this.getTree();
    },
  },
};
</script>
